package com.twotiger.and.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.activity.account.investtransfer.InverstedTransferDetailPage;
import com.twotiger.and.activity.account.investtransfer.InvestTransferPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.DebtCanTrans;
import com.twotiger.and.bean.DebtHasBuy;
import com.twotiger.and.bean.DebtTransed;
import com.twotiger.and.bean.DebtTransing;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.LogUtil;

/**
 * 投资转让的adapter
 * Created by victor on 2014/11/7 0007.
 */
public class InvestTransferAdapter extends BaseAdapter {
    private BaseActivity mactivity;
    private ArrayList<Basebean> mList;

    public InvestTransferAdapter(Activity _Activity) {
        this.mactivity = (BaseActivity) _Activity;
        this.mList = new ArrayList<Basebean>();
    }

    public void addData(List<Basebean> invests) {
        if (!ListUtils.isEmpty(invests)) {
            mList.addAll(invests);
        }
        notifyDataSetChanged();
    }

    public void resetData(List<Basebean> invests) {
        mList.clear();
        if (!ListUtils.isEmpty(invests)) {
            mList.addAll(invests);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (!ListUtils.isEmpty(mList)) {
            return mList.size();
        }
        return 1;
    }

    @Override
    public Basebean getItem(int position) {
        if (!ListUtils.isEmpty(mList) && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Basebean invest = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mactivity).inflate(R.layout.invest_trans_list_item,
                null);
        }
        final TextView tv_top_left = ViewHolder.get(convertView, R.id.tv_top_left);
        final TextView project_name = ViewHolder.get(convertView, R.id.project_name);
        final TextView tag_discount = ViewHolder.get(convertView, R.id.tag_discount);
        final TextView tag_discount_left = ViewHolder.get(convertView, R.id.tag_discount_left);
        final TextView tv_middle_left = ViewHolder.get(convertView, R.id.tv_middle_left);
        final TextView tv_middle_right = ViewHolder.get(convertView, R.id.tv_middle_right);
        final TextView tv_bottom_left = ViewHolder.get(convertView, R.id.tv_bottom_left);
        final TextView tv_bottom_right = ViewHolder.get(convertView, R.id.tv_bottom_right);
        final RelativeLayout rl_bottom_layout = ViewHolder.get(convertView, R.id.rl_bottom_layout);
        final TextView tv_trans_endtime = ViewHolder.get(convertView, R.id.tv_trans_endtime);
        final ImageView iv_right = ViewHolder.get(convertView, R.id.iv_right);
        final TextView tv_unit_1 = ViewHolder.get(convertView, R.id.tv_unit_1);
        final TextView tv_unit_2 = ViewHolder.get(convertView, R.id.tv_unit_2);

        if (invest instanceof DebtCanTrans) { //可转让标签
            iv_right.setVisibility(View.INVISIBLE);
            tag_discount.setVisibility(View.INVISIBLE);
            tag_discount_left.setVisibility(View.GONE);
            project_name.setVisibility(View.INVISIBLE);
            DebtCanTrans mDebtCanTrans = (DebtCanTrans) invest;
            rl_bottom_layout.setVisibility(View.GONE);
            tv_top_left.setText(mDebtCanTrans.getName());
            tv_middle_left.setText(mDebtCanTrans.getRemainDays() + "");
            tv_middle_right
                .setText(ArithUtils.coverMoneyComma(mDebtCanTrans.getInvestAmount() + ""));
            tv_bottom_left.setText("项目周期");
            tv_bottom_right.setText("投资金额");
            tv_unit_1.setText("天");
            tv_unit_2.setText("元");

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtil.info("点击了啊啊啊");
                }
            });

        } else if (invest instanceof DebtTransing) { //转让中标签
            iv_right.setVisibility(View.VISIBLE);
            tag_discount.setVisibility(View.INVISIBLE);
            project_name.setVisibility(View.INVISIBLE);
            final DebtTransing mDebtTransing = (DebtTransing) invest;
            if(mDebtTransing.getDiscountMark() == 0){
                tag_discount_left.setVisibility(View.GONE);
            }else if(mDebtTransing.getDiscountMark() == 1){
                tag_discount_left.setVisibility(View.VISIBLE);
                tag_discount_left.setText(ArithUtils.coverMoney(mDebtTransing.getAssignRate() / 10 + "")
                    + "折");
            }
            rl_bottom_layout.setVisibility(View.VISIBLE);
            tv_top_left.setText(mDebtTransing.getName());
            tv_trans_endtime.setText("" + DateUtil.getlongtoDate(mDebtTransing.getDeadline()));
            tv_middle_left
                .setText(ArithUtils.coverMoneyComma(mDebtTransing.getInvestAmount() + ""));
            tv_middle_right
                .setText(ArithUtils.coverMoneyComma(mDebtTransing.getCompletedVal() + ""));
            tv_bottom_left.setText("投资金额");
            tv_bottom_right.setText("已转本金");
            tv_unit_1.setText("元");
            tv_unit_2.setText("元");

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent_debtTransing = new Intent(mactivity,
                        InverstedTransferDetailPage.class);
                    intent_debtTransing.putExtra("TYPE", InvestTransferPage.TRANSING);
                    intent_debtTransing.putExtra("DETAIL", mDebtTransing);
                    mactivity.startNewActivity(intent_debtTransing, R.anim.push_left_in,
                        R.anim.push_left_out, false);
                }
            });

        } else if (invest instanceof DebtTransed) { //已完成标签
            iv_right.setVisibility(View.VISIBLE);
            tag_discount.setVisibility(View.INVISIBLE);
            project_name.setVisibility(View.INVISIBLE);
            final DebtTransed mDebtTransed = (DebtTransed) invest;
            if(mDebtTransed.getDiscountMark() == 0){
                tag_discount_left.setVisibility(View.GONE);
            }else if(mDebtTransed.getDiscountMark() == 1){
                tag_discount_left.setVisibility(View.VISIBLE);
                tag_discount_left.setText(ArithUtils.coverMoney(mDebtTransed.getAssignRate() / 10 + "")
                    + "折");
            }
            rl_bottom_layout.setVisibility(View.GONE);
            tv_top_left.setText(mDebtTransed.getName());
            tv_middle_left.setText(ArithUtils.coverMoneyComma(mDebtTransed.getCompletedVal() + ""));
            tv_middle_right.setText(ArithUtils.coverMoney(mDebtTransed.getCompletedVal() + ""));
            tv_bottom_left.setText("投资金额");
            tv_bottom_right.setText("已转本金");
            tv_unit_1.setText("元");
            tv_unit_2.setText("元");

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent_debtTransing = new Intent(mactivity,
                        InverstedTransferDetailPage.class);
                    intent_debtTransing.putExtra("TYPE", InvestTransferPage.TRANSED);
                    intent_debtTransing.putExtra("DETAIL", mDebtTransed);
                    mactivity.startNewActivity(intent_debtTransing, R.anim.push_left_in,
                        R.anim.push_left_out, false);
                }
            });

        } else if (invest instanceof DebtHasBuy) { //已购买标签
            iv_right.setVisibility(View.VISIBLE);
            project_name.setVisibility(View.VISIBLE);
            final DebtHasBuy mDebtHasBuy = (DebtHasBuy) invest;
            rl_bottom_layout.setVisibility(View.GONE);
            tag_discount_left.setVisibility(View.GONE);
            if (mDebtHasBuy.getDiscountMark() == 0) {
                tag_discount.setVisibility(View.INVISIBLE);
            } else if (mDebtHasBuy.getDiscountMark() == 1) {
                tag_discount.setVisibility(View.VISIBLE);
                tag_discount.setText(ArithUtils.coverMoney(mDebtHasBuy.getAssignRate() / 10 + "")
                                     + "折");
            }
            tv_top_left.setText(DateUtil.getDateStringForLong(
                Long.valueOf(mDebtHasBuy.getInvestTime()), DateUtil.DATE_FMT));
            project_name.setText(mDebtHasBuy.getName());
            tv_middle_left.setText(mDebtHasBuy.getRemainDays() + "");
            tv_middle_right.setText(ArithUtils.coverMoneyComma(mDebtHasBuy.getInvestAmount() + ""));
            tv_bottom_left.setText("剩余期限");
            tv_bottom_right.setText("购买金额");
            tv_unit_1.setText("天");
            tv_unit_2.setText("元");

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent_debtTransing = new Intent(mactivity,
                        InverstedTransferDetailPage.class);
                    intent_debtTransing.putExtra("TYPE", InvestTransferPage.HASBUY);
                    intent_debtTransing.putExtra("DETAIL", mDebtHasBuy);
                    mactivity.startNewActivity(intent_debtTransing, R.anim.push_left_in,
                        R.anim.push_left_out, false);
                }
            });

        }

        return convertView;
    }
}
